# gg_scatter
library(ggplot2)
library(GGally)
library(ggthemes)

# Basic scatter----
ggplot(diamonds, aes(carat, price)) + geom_point()

ggplot(diamonds, aes(carat, price)) + geom_point(aes(shape = cut))

ggplot(diamonds, aes(carat, price)) + geom_point(aes(colour = cut))

ggplot(diamonds, aes(carat, price)) + geom_point(aes(colour = cut, size = table))

ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 1/10)

ggplot(diamonds, aes(carat, price)) + geom_point(shape = '.')

# Decorating----
p <- ggplot(diamonds, aes(carat, price)) + geom_point(aes(colour = cut))

p + ggtitle("Scatter Plot") +
  theme_economist(base_size = 14) +
  scale_colour_economist() +
  guides(size = guide_legend(title = NULL), colour = guide_legend(title = NULL))

p1 <- ggplot(diamonds, aes(carat, price)) + 
  geom_point(aes(colour = cut, size = table)) +
  ggtitle('Scatter Plot')

p2 <- p1 + theme_wsj(base_size = 12) + scale_colour_wsj() +
  guides(size = guide_legend(title = NULL), colour = guide_legend(title = NULL))
p2

p2 + xlim(1, 2) + ylim(5000, 10000)

# Data exploring ----
ggscatmat(trees)

# ggscatmat
data("iris")
ggscatmat(iris, columns = 1:4, color = 'Species')

# ggparis
data(tips, package = 'reshape')
pairs(tips[, 1:3])

pm1 <- ggpairs(
  tips[, c(1, 3, 4, 2)],
  mapping = ggplot2::aes(
    color = sex,
    shape = sex,
    alpha = 0.4),
  upper = list(
    continous = 'cor',
    combo = 'box',
    discrete = 'facetbar'),
  lower = list(
    continous = 'point',
    combo = 'facethist',
    discrete = 'facetbar'),
  diag = list(continous = 'densityDiag', discrete = 'barDiag')
)
pm1

##
pm2 <- ggpairs(iris[, c(1, 2, 5, 3, 4)],
               mapping = ggplot2::aes(color = Species, shape = Species, alpha = 0.4),
               upper = list(continous = 'density', combo = 'box'),
               lower = list(continous = 'points', combo = 'dot'),
               title = 'Iris', axisLabels = 'internal')
pm2

##
data(diamonds, package = "ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1], 200), ]
pm3 <- ggpairs(
  diamonds.samp[, 1:5],
  mapping = ggplot2::aes(color = cut),
  upper = list(continous = wrap('density', alpha = 0.5), combo = 'box'),
  lower = list(contnous = wrap('smooth', alpha = 0.3), combo = wrap('dot', alpha = 0.4)),
  title = "Diamonds"
)
pm3
